﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class NhaXuatBanRepository
    {
        public List<NhaXuatBanDTO> LayDanhSachNXB()
        {
            List<NhaXuatBanDTO> list = new List<NhaXuatBanDTO>();
            try
            {
                QLTVEntities contex = ContextFactory.GetInstance();
                var query = from nxb in contex.NHA_XUAT_BANList
                            select nxb;
                foreach (var nxbEF in query)
                {
                    NhaXuatBanDTO nxbDTO = new NhaXuatBanDTO();
                    nxbDTO.MaNhaXuatBan = nxbEF.MaNhaXuatBan;
                    nxbDTO.TenNhaXuatBan = nxbEF.TenNhaXuatBan;
                    list.Add(nxbDTO);
                }
            }
            catch (System.Exception)
            {
            }
            return list;
        }

        public void ThemNhaXuatBan(NhaXuatBanDTO nxbDTO)
        {
            try
            {
                QLTVEntities contex = ContextFactory.GetInstance();
                NHA_XUAT_BAN nxbEF = new NHA_XUAT_BAN();
                nxbEF.TenNhaXuatBan = nxbDTO.TenNhaXuatBan;
                contex.AddToNHA_XUAT_BANList(nxbEF);
                contex.SaveChanges();
            }
            catch (System.Exception)
            {
            }
        }

        public void CapNhatNXB(NhaXuatBanDTO nxbDTO)
        {
            try
            {
                QLTVEntities contex = ContextFactory.GetInstance();
                NHA_XUAT_BAN nxbEF = new NHA_XUAT_BAN();
                nxbEF.MaNhaXuatBan = nxbDTO.MaNhaXuatBan;
                nxbEF.TenNhaXuatBan = nxbDTO.TenNhaXuatBan;
                contex.NHA_XUAT_BANList.ApplyCurrentValues(nxbEF);
                contex.SaveChanges();
            }
            catch (System.Exception)
            {
            }
        }

        public int LayMaNXBMax()
        {
            QLTVEntities context = ContextFactory.GetInstance();
            int maNXB = context.NHA_XUAT_BANList.Max(s => s.MaNhaXuatBan);

            return maNXB;
        }
                
        public NhaXuatBanDTO LayThongTinNXB(int maNXB)
        {
            NhaXuatBanDTO nxbDTO = new NhaXuatBanDTO();
            try
            {
                QLTVEntities context = ContextFactory.GetInstance();
                var query = from nxb in context.NHA_XUAT_BANList
                            where nxb.MaNhaXuatBan == maNXB
                            select nxb;
                NHA_XUAT_BAN nxbTemp = query.First();
                if (nxbTemp != null)
                {
                    nxbDTO.MaNhaXuatBan = nxbTemp.MaNhaXuatBan;
                    nxbDTO.TenNhaXuatBan = nxbTemp.TenNhaXuatBan;
                }
            }
            catch (System.Exception)
            {

            }
            return nxbDTO;
        }
    }
}
